package com.application.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.launcher.ARouter;
import com.application.BR;
import com.application.R;
import com.application.base.BaseFragment;
import com.application.databinding.FragmentMainBinding;
import com.application.util.ARouterConstants;
import com.application.viewmodel.MainViewModel;

public class MainFragment extends BaseFragment<FragmentMainBinding, MainViewModel> {

    private static final String TAG = "MainFragment";
    private static final String ARG_USERNAME = "username";
    public MainFragment() {
    }
    public static MainFragment newInstance(String username) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_USERNAME, username);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    protected int initLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    protected int initVariableId() {
        return BR.mainViewModel;
    }

    @Override
    protected void initData() {
        super.initData();
        binding.setMain(this);
        initUI();
    }

        private void initUI() {
        binding.tvSignature.setSelected(true);

        binding.buttonChange.setOnClickListener(v -> {
            binding.tvSignature.setText("像风翻书一样翻过大好时光，每一页都藏着不期而遇的光");
            Toast.makeText(getContext(), "更换签名成功!", Toast.LENGTH_SHORT).show();
        });
    }

//    private void handleArguments() {
//        if (getArguments() != null) {
//            String username = getArguments().getString(ARG_USERNAME);
//            if (username != null && !username.isEmpty()) {
//                Toast.makeText(getContext(), "欢迎 " + username + " 登录", Toast.LENGTH_LONG).show();
//            }
//        }
//    }

    public void jumpToEvaluation(View view) {
        ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_EVALUATION)
                .navigation(getActivity());
    }

    public void jumpToHandler(View view) {
        ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_HANDLER)
                .navigation(getActivity());
    }

    public void jumpToRetrofitRxJava(View view) {
        ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_RETROFIT_BY_JAVA)
                .navigation(getActivity());
    }

    public void jumpToLiveEventBus(View view) {
        ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_LIVE_EVENT_BUS)
                .navigation(getActivity());
    }

//    public void jumpToRecyclerView(View view) {
//        ARouter.getInstance()
//                .build(ARouterConstants.ACTIVITY_URL_RECYCLER_VIEW)
//                .navigation(getActivity());
//    }

    public void jumpToSQLiteMain(View view){
        ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_SQLITE_MAIN)
                .navigation(getActivity());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 清除绑定，防止内存泄漏
        binding = null;
        Log.d(TAG, "onDestroyView: 视图已销毁");
    }
}